angular.module('Controller')

.controller('QgyRentBillManageCtrl', ($scope,NgTableParams,menuManage,wfAlert,$localStorage,commonUtil,commonConstant,$rootScope,$uibModal,$http) ->
    initSearchData = ()->
        $scope.search = {
            type:undefined ,
            apartmentId:$localStorage['accountStore'],
            payTimeStart:undefined,
            status:undefined
            payTimeEnd:undefined
            filterType:undefined
            searchStr:undefined
            contractStatus:8
        }
    initSearchData()
    $scope.enabledList=commonConstant.enabledList(true)
    $scope.payWayList=commonConstant.payWayList(true)
    $scope.billStatusList=commonConstant.billStatusList(true)
    $scope.appointmentStatusList=commonConstant.appointmentStatusList(true)
    $scope.contractStatusList=commonConstant.contractStatusList(true)
    loadApartment=()->
        $scope.apartmentList=[{id:undefined,name:'全部'}]
        $http.get('/apartment',params:{page:0,size:999,enabled:true})
        .success (resp)->
            if resp.code==0
                $scope.apartmentList=$scope.apartmentList.concat(resp.message.content)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    loadApartment()
    $scope.tableParams = new NgTableParams(
            {page: 1, count: commonConstant.pageTableCount},
            {
                counts: [5, 10, 20],
                getData: ($defer, params)->
                    $scope.doQuery($defer,params)
                    return ;
            }
    )
    $scope.doQuery=($defer, params)->
        reqData={
            page: params.page() - 1,
            size: params.count(),
            type:1
            apartmentId:$scope.search.apartmentId,
            status:$scope.search.status
            payTimeEnd:$scope.search.payTimeEnd
            searchStr:$scope.search.searchStr
            contractStatus:$scope.search.contractStatus
        }
        if $scope.search.filterType=='near'
            reqData.startTimeBegin=moment().add(-5, 'day').format('YYYY-MM-DD')
            reqData.startTimeEnd=moment().format('YYYY-MM-DD')
        else if $scope.search.filterType=='own'
            reqData.startTimeEnd=moment().add(-1, 'day').format('YYYY-MM-DD')
        else
            if $scope.search.payTimeStart
                reqData.payTimeStart=moment($scope.search.payTimeStart).format('YYYY-MM-DD')
            if $scope.search.payTimeEnd
                reqData.payTimeEnd=moment($scope.search.payTimeEnd).format('YYYY-MM-DD')
        $http.get('/bill',params:reqData)
        .success (resp)->
            if resp.code==0
                $scope.tableData=resp.message.content
                params.total(resp.message.totalElements)
                $defer.resolve($scope.tableData)
            else
                console.log(resp.message)
        .error (err)->
            console.log(err)
    _.extend $scope,
        newData: ->
            data={type:1,enabled:1}
            showModal('new',data)
        editData:(item)->
            theData=angular.copy(item)
            showModal('edit',theData)
        viewData:(item)->
            return
            theData=angular.copy(item)
            showModal('view',theData)
        resetSearch: ->
            initSearchData()
        queryData: (keepPage)->
            if(keepPage != true)
                $scope.tableParams.page(1)
            $scope.tableParams.reload()
        needWarn:(item)->
            if item.status!=1
                return false
            else if item.startTime==undefined
                return false
            else
                today=moment(new Date())
                if moment(item.startTime).diff(today,'days')<5
                    return true
                else
                    return false
        warnBill:(item)->
            $http.post("/bill/#{item.id}/sms")
            .success((resp)->
                if resp.code==0
                    wfAlert.toastInfo('催款通知已发送')
            )
            ''
        warnSMS:(item)->
            smsText=item.apartmentName+'房间:'+item.houseNo+'从'+
                    moment(item.startTime).format('YYYY年MM月DD日')+'至'+
                    moment(item.endTime).format('YYYY年MM月DD日')+'的租金账单已生成，付费截止时间：'+
                    moment(item.startTime).format('YYYY年MM月DD日')+'。请在付费截止时间之前及时支付租金，谢谢您的支持。'
            str='确认向用户发送催款信息【'+smsText+'】吗？'
            return str
        payBill:(item)->
            showModal('pay',item)
        filterCustom:(type)->
            if $scope.search.filterType==type
                $scope.search.filterType=undefined
            else
                $scope.search.filterType=type
                $scope.search.status=1
    showModal=(type,data)->
        $scope.modal = $uibModal.open({
            animation: true,
            appendTo: $("#modalContent:eq(0)"),
            controller: 'QgyRentPayModalCtrl',
            templateUrl: 'templates/business-manage/rent-pay-modal/rent-pay-modal.html',
            resolve: {
                items:  ()->{type: type, data: data}
            }
        })
        $scope.modal.result.then((modalData)->
            $scope.queryData()
        )
)